<?php

class Controller 
{
	var $db;

	// constructor of Controller
	function Controller()
	{
		// load the database with the standard SQLite3 object
		//$this->db = new SQLite3("../db/data.sqlite");
	}

	/* load the view template */
	function load($file, $data, $layout = true)
	{

		// assign the according data in view template
		foreach (array_keys($data) as $key)
		{
			eval('$' . $key . '= $data['.$key.'];');
		}

		// display the view in ./views/ folder
		$fileContents = file_get_contents("../views/$file");
		if ($layout)
			eval("?>".  $fileContents. "<?");
		else
			return $fileContents;
	}

	function loadLibrary($file)
	{
		include_once("../library/$file");
	}
	function loadController($file)
	{
		include_once "$file";
	}

	function getElementsByTagName($html, $tag)
	{
		$pattern = "/(<".$tag."[^>]*>.*<\/".$tag.">)/siU";
		preg_match($pattern ,$html, $regs);
		
		return $regs;
	}

	function removeElementsByTagName($html, $tagToRemove)
	{
		$regex = "/<".$tagToRemove."[^>]*>.*<\/".$tagToRemove.">/siU";
		$html = preg_replace($regex,'', $html);
		return $html;
	}

    /**
     * strip_selected_tags ( string str [, string strip_tags[, strip_content flag]] )
     * ---------------------------------------------------------------------
     * Like strip_tags() but inverse; the strip_tags tags will be stripped, not kept.
     * strip_tags: string with tags to strip, ex: "<a><p><quote>" etc.
     * strip_content flag: TRUE will also strip everything between open and closed tag
     */
	function strip_selected_tags($str, $tags = "", $stripContent = false)
    {
        preg_match_all("/<([^>]+)>/i",$tags,$allTags,PREG_PATTERN_ORDER);
        foreach ($allTags[1] as $tag){
            if ($stripContent) {
                $str = preg_replace("/<".$tag."[^>]*>.*<\/".$tag.">/iU","",$str);
            }
            $str = preg_replace("/<\/?".$tag."[^>]*>/iU","",$str);
        }
        return $str;
    }

	function real_strip_tags($i_html, $i_allowedtags = array(), $i_trimtext = FALSE) 
	{
		if (!is_array($i_allowedtags))
			$i_allowedtags = !empty($i_allowedtags) ? array($i_allowedtags) : array();
		$tags = implode('|', $i_allowedtags);

		if (empty($tags))
			$tags = '[a-z]+';

		preg_match_all('@</?\s*(' . $tags . ')(\s+[a-z_]+=(\'[^\']+\'|"[^"]+"))*\s*/?>@i', $i_html, $matches);

		$full_tags = $matches[0];
		$tag_names = $matches[1];

		foreach ($full_tags as $i => $full_tag) {
			if (!in_array($tag_names[$i], $i_allowedtags))
				if ($i_trimtext)
					unset($full_tags[$i]);
				else
					$i_html = str_replace($full_tag, '', $i_html);
		}

		return $i_trimtext ? implode('', $full_tags) : $i_html;
	}
	
}
